package com.msl.debezium.controller;

import com.msl.debezium.common.Msg;
import com.msl.debezium.common.ResultCodeEnum;
import com.msl.debezium.dto.UserDto;
import com.msl.debezium.request.QueryUserListReq;
import com.msl.debezium.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@Validated
@Slf4j
@RequestMapping("/cdc/user")
@Api(value="查询用户信息", tags = "UserController")
@ApiResponses({@ApiResponse(code = 200, message = "OK,请求成功"),@ApiResponse(code = 400, message = "bad-request,客户端请求错误"),
               @ApiResponse(code = 401, message = "url被禁止访问"),@ApiResponse(code = 403, message = "用户未授权访问"),
               @ApiResponse(code = 500, message = "服务器内部错误")})
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 如果想用参数作为路径可以直接添加/{id}
     * required = false是默认的，表示不是必须的入参，如果需要必填在ApiImplicitParam中改为true即可
     * @ModelAttribute 不添加会报错，用以swagger识别RequstBody
     * @ApiIgnore 使用RequestBody后会多显示一个json参数窗口，其实并未使用该参数体，这个需要屏蔽
     * dataTypeClass = QueryUserListReq.class
     */
    @PostMapping("/queryUserList/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="用户ID",example = "185", required = true, paramType = "JSON"),
            @ApiImplicitParam(name="username", value="用户姓名", example="min敏", allowEmptyValue = true, paramType = "JSON"),
            @ApiImplicitParam(name="updated_at", value="修改时间", example="2021-1-1", allowEmptyValue = true, paramType = "JSON")
    })
    @ApiOperation(value = "queryUserListById",notes = "queryUserListById")
    public Msg<List<UserDto>> queryUserList(@ModelAttribute @ApiIgnore @RequestBody QueryUserListReq req) throws Exception {
        Msg<List<UserDto>> msg = Msg.ok();
        List<UserDto> userList = userService.queryUserList(req);
        msg.setData(userList);
        return msg;
    }

    /**
     * dataType="QueryUserListReq"指定body类，用以显示参数信息
     */
    @PostMapping("/queryUserList")
    @ApiImplicitParams({
            @ApiImplicitParam(name="reqBody",value="报文",dataType="QueryUserListReq",required=true)
    })
    @ApiOperation(value = "queryUserList",notes = "queryUserList")
    public Msg<List<UserDto>> queryUserList2(@RequestBody @Valid QueryUserListReq reqBody){
        Msg<List<UserDto>> msg = Msg.ok();
        if(reqBody.getId() == null){
            return new Msg(ResultCodeEnum.FAIL,"用户ID不能为空");
        }
        try{
            List<UserDto> userList = userService.queryUserList(reqBody);
            msg.setData(userList);
        }catch(Exception e){
            Msg.err(ResultCodeEnum.SERVER_ERROR,"未知异常，请联系管理员");
        }
        return msg;
    }

}
